package com.hjw.service.impl;

import com.hjw.dao.OrderDao;
import com.hjw.entity.Order;
import com.hjw.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @创建时间：2025-04-21
 * @文件名称：satoken_sobuk - com.hjw.service.impl
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RedisTemplate<String, Order> redisTemplate;

    @Autowired
    private OrderDao orderDao;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public int add(Order order) {
        return orderDao.add(order);
    }

    @Override
    public List<Order> findByCondition(Map<String, Object> map) {
        return orderDao.findByCondition(map);
    }

    @Override
    public int getCountByCondition(Map<String, Object> map) {
        return orderDao.getCountByCondition(map);
    }

    @Override
    public List<Order> findMyOrder(Map<String, Object> map) {
        if (map.get("uid") == null){
            return null;
        }
        return orderDao.findMyOrder(map);
    }

    @Override
    public Order findByOrdId(Map<String, Object> map) {
        if (map.get("uid") == null || map.get("ordid") == null){
            return null;
        }
        Order order = redisTemplate.opsForValue().get("order:" + map.get("ordid"));
        if (order != null){
            log.info("获取了redis缓存订单信息");
            return order;
        }
        log.info("数据库查询订单信息");
        order = orderDao.findByOrdId(map);
        if (order != null){
            log.info("redis缓存订单信息");
            redisTemplate.opsForValue().set("order:" + map.get("ordid"), order,20, TimeUnit.MINUTES);
        }
        return order;
    }

    @Override
    public Order findById(Integer id) {
        return orderDao.findById(id);
    }

    @Override
    public int update(Map<String, Object> map) {
        if (map.get("ordid") == null){
            return 0;
        }
        redisTemplate.delete("order:" + map.get("ordid"));
        int i = orderDao.update(map);
        redisTemplate.delete("order:" + map.get("ordid"));
        return i;
    }

    @Override
    public Double findByMouth(Integer year, Integer month) {
        return orderDao.findByMouth(year, month);
    }

    @Override
    public Double findByMouthMoney(Integer year, Integer month) {
        return orderDao.findByMouthMoney(year, month);
    }

    @Override
    public int getPayStatusCount(int payStatus) {
        return orderDao.getPayStatusCount(payStatus);
    }

    @Override
    public int getShipStatusCount(int shipStatus) {
        return orderDao.getShipStatusCount(shipStatus);
    }


}
